/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
import javax.swing.*;
import java.awt.event.*;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.table.DefaultTableModel;
import java.util.ArrayList;
/**
 *
 * @author sergi
 */
public class VistaCorregir extends JPanel {
    private JScrollPane jspTblCorregir;
    private JScrollPane jsptxt;
    private JButton btnMostrar;
    private JButton btnSiguiente;
    private JButton btnAceptartodo;
    private JButton btnAceptar;
    private JTable tblPalabras;
    private JTable palincorrecta;
    private JLabel lbk;
    private JTextField txtK;
    private SpringLayout layout;
    private SpringLayout layoutBotones;
    private JProgressBar jpbMostrarK;
    private JPanel botones;
    private CICorregir cic;
    private int k;
    private int pos;
    private boolean mostrat = false;
    private ArrayList<String> kpos = new ArrayList();
    private ArrayList<String> a;
    private AccionProgreso accionCorregir;
    private DefaultTableModel dtm3 = new DefaultTableModel() {
    @Override
    public boolean isCellEditable (int fila, int columna) {
        return false;
    }
    };

    public VistaCorregir(CICorregir a) {
        cic = a;
        inicialitzar();
    }

    private void inicialitzar() {
        palincorrecta = new JTable();
        jspTblCorregir = new JScrollPane();
        jsptxt = new JScrollPane();
        btnMostrar = new JButton("Mostrar k posibles");
        btnSiguiente = new JButton("Siguiente palabra");
        btnAceptar = new JButton("Cambiar palabra");
        btnAceptartodo = new JButton("Cambiar todas");
        tblPalabras = new JTable();
        layout = new SpringLayout();
        botones = new JPanel();
        layoutBotones = new SpringLayout();
        tblPalabras.setModel(dtm3);
        jpbMostrarK = new JProgressBar();
        lbk = new JLabel();
        txtK = new JTextField();

        btnSiguiente.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
             siguienteActionPerformed(e);
        }
        });
        btnAceptartodo.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
             aceptartodoActionPerformed(e);
        }
        });
        btnAceptar.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
             aceptarActionPerformed(e);
        }
        });
        btnMostrar.addActionListener(new ActionListener(){

        public void actionPerformed(ActionEvent e) {
             mostrarActionPerformed(e);
        }
        });
        jpbMostrarK.setPreferredSize(new Dimension(20,20));
        jpbMostrarK.setStringPainted(true);
        
        inicializarpalincorrecta();
        inicializarTabla();
        inicializarPanel();
        inicializarLayout();
        inicializarLayoutBotones();
        montar();
    }

    public void corregir() {
        a = cic.getinc();
        pos = 0;
        eliminartabla();
        if (pos < a.size()) {
            palincorrecta.setValueAt(a.get(pos), 0, 0);
            cic.subrayar(a.get(pos), 1);
        }
        else palincorrecta.setValueAt("Corrección finalizada", 0, 0);
   }

   private void eliminartabla() {
        int h;
        int tam = dtm3.getRowCount();
        for(h = 0; h < tam; ++h) dtm3.removeRow(dtm3.getRowCount()-1);
   }

   private void siguienteActionPerformed(ActionEvent e) {
        eliminartabla();
        ++pos;
        if (pos < a.size()) {
            palincorrecta.setValueAt(a.get(pos), 0, 0);
            int rep = 1;
            int h;
            for(h = 0; h < pos; ++h) {
                if(a.get(h).equals(a.get(pos))) ++rep;
            }
            cic.subrayar(a.get(pos), rep);
        }
        else {
            palincorrecta.setValueAt("Corrección finalizada", 0, 0);
            eliminartabla();
        }
        mostrat = false;
   }

   private void aceptarActionPerformed(ActionEvent e) {
       if (mostrat == true) {
           int[] sel = tblPalabras.getSelectedRows();
           if (sel.length == 1) {
               int rep = 1;
               int h;
               for(h = 0; h < pos; ++h) {
                   if(a.get(h).equals(a.get(pos))) ++rep;
               }
               String s = tblPalabras.getValueAt(sel[0], 0).toString();
               cic.cambiarsola(a.get(pos), s, rep);
               a.set(pos, "");
           }
       }
       cic.actualizarlong();
   }

   private void aceptartodoActionPerformed(ActionEvent e) {
       int i;
       if (mostrat == true) {
           int[] sel = tblPalabras.getSelectedRows();
           if (sel.length == 1) {
               String s = tblPalabras.getValueAt(sel[0], 0).toString();
               cic.cambiartodo(a.get(pos), s);
               String aux = a.get(pos);
               i = pos + 1;
               while(i < a.size()) {
                    if(a.get(i).equalsIgnoreCase(aux)) a.remove(i);
                    else ++i;
               }
           }
       }
       cic.actualizarlong();
   }

   private void accionMostrar(){
 
       try {
            mostrat = true;
            String ktext = txtK.getText();
            if (ktext.isEmpty()) mensaje ("introduce la k por favor");
            k = Integer.parseInt(ktext);
            ArrayList<StringDouble> ksd = new ArrayList<StringDouble>();
            ksd = cic.getkposibles(a.get(pos), k);
            kpos.clear();
            for(int l = 0; l < ksd.size(); ++l) kpos.add(ksd.get(l).s);
            StringDouble[] s = new StringDouble[kpos.size()];
            int j;
            for(j = 0; j < s.length; ++j) {
                String aux = ksd.get(j).s;
                int h = 0;
                while(h < aux.length() && aux.charAt(h) > '!') ++h;
                s[j] = new StringDouble(aux.substring(0,h), ksd.get(j).d);
            }
            eliminartabla();

            StringDouble[] s1 = new StringDouble[s.length];
            int f;
            for(f = 0; f < s.length; ++f) s1[f] = s[s.length - f - 1];
            int i;
            for(i = 0; i < s1.length; ++i) {
                String[] s2 = new String[2];
                s2[0] = s1[i].s;
                s2[1] = String.valueOf(s1[i].d*100);
                dtm3.addRow(s2);
                //tblPalabras.setValueAt(s1[i].s, i, 0);
                //tblPalabras.setValueAt(s1[i].d, i, 1);
            }
        } catch(Exception ex) {
            System.out.println("Excepcion");
        }
   }
   private void mostrarActionPerformed(ActionEvent e) {
      accionCorregir = new AccionProgreso(jpbMostrarK,cic.getProgresoAlg(),null){

            public void run() {
                accionMostrar();
            }
      
      };
      accionCorregir.setMilis(200);
      accionCorregir.ejecutarYMonitorizar();
   }

   public void finalizar() {
        pos = 0;
        palincorrecta.setValueAt("fi", 0, 0);
        eliminartabla();
   }
   public void mensaje(String mensaje){
        mensaje(this,mensaje);
    }
    public void mensaje(Component cent, String mensaje){
        JOptionPane.showMessageDialog(cent, mensaje);
    }

    private void inicializarPanel(){
        this.setBorder(BorderFactory.createTitledBorder(BorderFactory.createLineBorder(Color.GRAY), "Corregir"));
        lbk.setSize(30,30);
        lbk.setText("K:");
        txtK.setSize(30,30);
        txtK.setText("");
    }
    private void inicializarpalincorrecta() {
        DefaultTableModel dtm4 = new DefaultTableModel() {
        @Override
        public boolean isCellEditable (int fila, int columna) {
            return true;
        }
        };
        palincorrecta.setModel(dtm4);

        jsptxt = new JScrollPane(palincorrecta);
        dtm4.addColumn("palabra incorrecta");

        palincorrecta.setModel(dtm4);


        String[] filaPrueba = new String[1];
        filaPrueba[0]="";


        dtm4.addRow(filaPrueba);
    }
    private void inicializarTabla() {
        jspTblCorregir = new JScrollPane(tblPalabras);
        dtm3.addColumn("k posibles");
        dtm3.addColumn("probabilidad (%)");
        tblPalabras.setModel(dtm3);
    }
    private void inicializarLayout(){
        this.setLayout(layout); 
        layout.putConstraint(SpringLayout.EAST, botones, 0, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, botones, 0, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.SOUTH, botones, 130, SpringLayout.NORTH, this);
        layout.putConstraint(SpringLayout.NORTH, botones, 0, SpringLayout.NORTH, this);

        layout.putConstraint(SpringLayout.EAST, jsptxt, -20, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, jsptxt, 20, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, jsptxt, 10, SpringLayout.SOUTH, botones);
        layout.putConstraint(SpringLayout.SOUTH, jsptxt, 44, SpringLayout.SOUTH, botones);

        layout.putConstraint(SpringLayout.EAST, jspTblCorregir, -20, SpringLayout.EAST, this);
        layout.putConstraint(SpringLayout.WEST, jspTblCorregir, 20, SpringLayout.WEST, this);
        layout.putConstraint(SpringLayout.NORTH, jspTblCorregir, 40, SpringLayout.SOUTH, jsptxt);
        layout.putConstraint(SpringLayout.SOUTH, jspTblCorregir, -10, SpringLayout.SOUTH, this);
        
        layout.putConstraint(SpringLayout.NORTH, jpbMostrarK, -30, SpringLayout.NORTH, jspTblCorregir);
        layout.putConstraint(SpringLayout.EAST, jpbMostrarK, 0, SpringLayout.EAST, jspTblCorregir);
        layout.putConstraint(SpringLayout.WEST, jpbMostrarK, 0, SpringLayout.WEST, jspTblCorregir);
    }
    private void inicializarLayoutBotones(){
        botones.setLayout(layoutBotones);
        layoutBotones.putConstraint(SpringLayout.WEST, lbk, 10, SpringLayout.WEST, botones);
        layoutBotones.putConstraint(SpringLayout.NORTH, lbk, 13, SpringLayout.NORTH, botones);
        
        layoutBotones.putConstraint(SpringLayout.WEST, txtK, 10, SpringLayout.EAST, lbk);
        layoutBotones.putConstraint(SpringLayout.NORTH, txtK, 13, SpringLayout.NORTH, botones);
        layoutBotones.putConstraint(SpringLayout.EAST, txtK, 50, SpringLayout.WEST, txtK);

        layoutBotones.putConstraint(SpringLayout.NORTH, btnMostrar, 10, SpringLayout.NORTH, botones);
        layoutBotones.putConstraint(SpringLayout.EAST, btnMostrar, -10, SpringLayout.EAST, botones);
        layoutBotones.putConstraint(SpringLayout.WEST, btnMostrar, 60, SpringLayout.WEST, txtK);

        layoutBotones.putConstraint(SpringLayout.NORTH, btnSiguiente, 30, SpringLayout.NORTH, btnMostrar);
        layoutBotones.putConstraint(SpringLayout.EAST, btnSiguiente, -10, SpringLayout.EAST, botones);
        layoutBotones.putConstraint(SpringLayout.WEST, btnSiguiente, 10, SpringLayout.WEST, botones);

        layoutBotones.putConstraint(SpringLayout.NORTH, btnAceptar, 30, SpringLayout.NORTH, btnSiguiente);
        layoutBotones.putConstraint(SpringLayout.WEST, btnAceptar, 10, SpringLayout.WEST, botones);
        layoutBotones.putConstraint(SpringLayout.EAST, btnAceptar, -10, SpringLayout.EAST, botones);

        layoutBotones.putConstraint(SpringLayout.NORTH, btnAceptartodo, 30, SpringLayout.NORTH, btnAceptar);
        layoutBotones.putConstraint(SpringLayout.WEST, btnAceptartodo, 10, SpringLayout.WEST, botones);
        layoutBotones.putConstraint(SpringLayout.EAST, btnAceptartodo, -10, SpringLayout.EAST, botones);
        
    }
    private void montar(){
        this.add(botones);
        this.add(jsptxt);
        this.add(jspTblCorregir);
        this.add(jpbMostrarK);
        botones.add(btnMostrar);
        botones.add(btnSiguiente);
        botones.add(btnAceptar);
        botones.add(btnAceptartodo);
        botones.add(txtK);
        botones.add(lbk);

    }
}
